package database

import (
	"gitee.com/mkwchecking/sr-core/log"
	"gitee.com/mkwchecking/sr-core/storage/database/driver"
	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"github.com/acmestack/gorm-plus/gplus"
	"gorm.io/gorm"
)

var (
	// DefaultDataSet is the default dataset.
	defaultDataSet = newDataSet()
)

func Install(cfg *driver.Config, logger log.Logger) (IDataSet, error) {
	db, err := NewGorm(cfg, logger)
	if err != nil {
		return nil, err
	}
	gplus.Init(db)
	defaultDataSet.SetDefault(db)
	return defaultDataSet, nil
}
func Dataset() IDataSet { return defaultDataSet }

type IDataSet interface {
	ymap.ISet[*gorm.DB]
}

func newDataSet() IDataSet {

	return &dataSet{
		container: ymap.NewSetMap[*gorm.DB](),
	}
}

type dataSet struct {
	container ymap.ISet[*gorm.DB]
}

// Delete implements *gorm.DBSet.
func (c *dataSet) Delete(key string) {
	c.container.Delete(key)
}

// Get implements *gorm.DBSet.
func (c *dataSet) Get(key string) (*gorm.DB, bool) {
	return c.container.Get(key)
}

// GetDefault implements *gorm.DBSet.
func (c *dataSet) GetDefault() (*gorm.DB, bool) {
	return c.container.GetDefault()
}

// Keys implements *gorm.DBSet.
func (c *dataSet) Keys() []string {
	return c.container.Keys()
}

// Set implements *gorm.DBSet.
func (c *dataSet) Set(key string, value *gorm.DB) {
	c.container.Set(key, value)
}

// SetDefault implements *gorm.DBSet.
func (c *dataSet) SetDefault(value *gorm.DB) {
	c.container.SetDefault(value)
}

// Values implements *gorm.DBSet.
func (c *dataSet) Values() []*gorm.DB {
	return c.container.Values()
}
